//
//  WLLNetTools.swift
//  AFN 网络封装
//
//  Created by 王乐乐 on 16/8/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

/**
 网络请求枚举
 
 - GET:  GET 请求
 - POST: POST 请求
 */
enum WLLNetToolsMethod :String{
    case GET = "GET"
    case POST = "POST"
}


/*
 - 对afn进行封装
 - 提供一个可以发送get和post请求的公共方法
 - 帮助当前app 请求所有接口数据
 */
class WLLNetTools: AFHTTPSessionManager {
    //全局访问点
    static let sharedTools :WLLNetTools = {
        let tools = WLLNetTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools

    }()
    
    
    /**
     网络请求公共方法
     
     - parameter method:     请求方式
     - parameter urlString:  请求urlString
     - parameter parameters: 请求参数
     - parameter success:    请求成功的闭包
     - parameter failure:    请求失败的闭包
     */
    func request (method:WLLNetToolsMethod,URLString: String, parameters: AnyObject?, success:(response : AnyObject?)->(), failure: (error:NSError) -> Void ){
        
        if method == .GET {
            GET(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response: response)
                }, failure: { (_, error) in
                    failure(error: error)
            })
        }else{
            POST(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response: response)
                }, failure: { (_, error) in
                    failure(error: error)
            })
        }
}


}

 //MARK: - 发布微博界面请求
extension WLLNetTools{
    /**
     发布文字微博
     */
    func composeUpdate (status:String,success:(response : AnyObject?)->(), failure: (error:NSError)->())  {
        //请求的 URL
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        //请求参数
        let params = [
            "access_token" : WLLUserAccountViewModel.shareTool.accessToken!,
            "status" : status
        ]
        //发送请求
        request(WLLNetToolsMethod.POST, URLString: urlString, parameters: params, success: success, failure: failure)
    }
    /**
     发布图片微博
     */
    func composeUpload (status:String,images:[UIImage], success:(response : AnyObject?)->(), failure: (error:NSError)->())  {
        
        //请求的 URL
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        //请求参数
        let params = [
            "access_token" : WLLUserAccountViewModel.shareTool.accessToken!,
            "status" : status
        ]
        
        //发送请求
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) in
        
            //二进制
            let data = UIImagePNGRepresentation(images.first!)!
            //done :这里怎么写的?  ...> 写在Controller里就有提示
            /**
             *  要上传的文件二进制
             *  服务器提供的名称
             *  保存到服务器指定文件夹的名字,但是一般客户端不需要,可随意填写
             *  上传的文件格式
             */
             formdata.appendPartWithFileData(data, name: "pic", fileName: "lele", mimeType: "application/octet-stream")
            }, progress: nil, success: { (_, response) in
                
                success(response: response)
                
            }) { (_, error) in
                
                failure(error: error)
                
        }
        
        
        
    }
    
    
    
    
}


 //MARK: - 微博首页
extension WLLNetTools{
    func loadHomeData(sinceId:Int64, maxId:Int64, success:(response : AnyObject?)->(), failure: (error:NSError) -> Void)  {
        //请求的 urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //TODO:这里模糊
        let params = [
            "access_token":WLLUserAccountViewModel.shareTool.userAccount!.access_token!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        request(WLLNetToolsMethod.GET, URLString: urlString, parameters: params, success: success, failure: failure)
        
    }
    
}


 //MARK: - 授权界面接口
extension WLLNetTools{
    /**
     通过 code 获取 access_token,uid
     */
    func oauthLoadUserAccount(code:String,success:(response : AnyObject?)->(), failure: (error:NSError) -> Void)  {
        //请求 urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let params = [
            "client_id":WBAppKey,
            "client_secret":WBAppSecret,
            "grant_type":"authorization_code",
            "code" : code,
            "redirect_uri" : WBRedirect_uri
        ]
request(WLLNetToolsMethod.POST, URLString: urlString, parameters: params, success: success, failure: failure)
        
    }
    /**
     请求参数access_token,uid    来请求个人信息
     */
    func oauthLoadUserInfo (userAccount:WLLUserAcountModel,success:(response : AnyObject?)->(), failure: (error:NSError) -> Void)  {
        //请求 ursString
        let urlString = "https://api.weibo.com/2/users/show.json"
        //请求参数(通过这些服务器指定的参数才能请求到数据)
        let params = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]

        request(WLLNetToolsMethod.GET, URLString: urlString, parameters: params, success: success, failure: failure)
        
        
    }
    
  
 
    
}


//MARK: - 请求天气接口
extension WLLNetTools {
    //封装了 url,
    func getWeatherData(success:(response : AnyObject?)->(),failure: (error:NSError) -> Void ) {
        let urlString = "http://www.weather.com.cn/data/sk/101010100.html"
        request(WLLNetToolsMethod.GET, URLString: urlString, parameters: nil, success: success, failure: failure)
    }
}

